package com.lcz.suanfa;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * <img width="128" height="110" src="https://tse3-mm.cn.bing.net/th/id/OIP-C.en-w_oH-yn2UsSRfWnOsGAHaGY?w=198&h=180&c=7&r=0&o=5&dpr=1.25&pid=1.7" alt="马刺">
 * 作者:     <a href="https://github.com/liuchuanzheng">刘传政</a>
 * 创建时间:  9:41 2022/3/4 0004
 * QQ:      1052374416
 * 电话:     18501231486
 * 描述:
 * </pre>
 */
public class 数学 {
    public List<String> fizzBuzz(int n) {
        ArrayList<String> strings = new ArrayList<>();
        for (int i = 1; i <= n; i++) {

            if (i % 3 == 0 && i % 5 != 0) {
                strings.add("Fizz");
            } else if (i % 3 != 0 && i % 5 == 0) {
                strings.add("Buzz");
            } else if (i % 3 == 0 && i % 5 == 0) {
                strings.add("FizzBuzz");
            } else {
                strings.add(i + "");
            }
        }
        return strings;
    }

    //给定整数 n ，返回 所有小于非负整数 n 的质数的数量 。
    public int countPrimes(int n) {

        int[] isPrime = new int[n];
        Arrays.fill(isPrime, 1);
        int ans = 0;
        for (int i = 2; i < n; ++i) {
            if (isPrime[i] == 1) {
                ans += 1;
                if ((long) i * i < n) {
                    for (int j = i * i; j < n; j += i) {
                        isPrime[j] = 0;
                    }
                }
            }
        }
        return ans;

    }

    public boolean isPrime(int x) {
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) {
                return false;
            }
        }
        return true;
    }

    public boolean isPowerOfThree(int n) {
        while (n != 0 && n % 3 == 0) {
            n /= 3;
        }
        return n == 1;
    }

    public int romanToInt(String s) {
        Map<String, Integer> roman = new HashMap<>();
        roman.put("I", 1);
        roman.put("V", 5);
        roman.put("X", 10);
        roman.put("L", 50);
        roman.put("C", 100);
        roman.put("D", 500);
        roman.put("M", 1000);
        int value = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            String s1 = String.valueOf(s.charAt(i));
            sb.append(s1);
            if (s1.equals("I")) {
                value = value + roman.get("I");
            } else if (s1.equals("V")) {
                value = value + roman.get("V");
                if (sb.toString().contains("I")) {
                    value = value - roman.get("I") * 2;
                }
            } else if (s1.equals("X")) {
                value = value + roman.get("X");
                if (sb.toString().contains("I")) {
                    value = value - roman.get("I") * 2;
                }
            } else if (s1.equals("L")) {
                value = value + roman.get("L");
                if (sb.toString().contains("X")) {
                    value = value - roman.get("X") * 2;
                }
            } else if (s1.equals("C")) {
                value = value + roman.get("C");
                if (sb.toString().contains("X")) {
                    value = value - roman.get("X") * 2;
                }
            } else if (s1.equals("D")) {
                value = value + roman.get("D");
                if (sb.toString().contains("C")) {
                    value = value - roman.get("C") * 2;
                }
            } else if (s1.equals("M")) {
                value = value + roman.get("M");
                if (sb.toString().contains("C")) {
                    value = value - roman.get("C") * 2;
                }
            }
        }
        return value;
    }
}
